package com.canomo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 价格计算Controller
 */
@Controller
@RequestMapping("price")
public class PriceController {

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("")
    public ModelAndView index() {
        ModelAndView model = new ModelAndView("price/priceIndex");
        return model;
    }


    /**
     * 计算利润
     * 总的公式:利润=(定价-亚马逊佣金-亚马逊物流费用-成本-运费)/定价
     * 亚马逊佣金:定价*15%
     * 亚马逊物流费用:大于和等于1.7cm的情况下收取3.19美金，低于和等于1.6的情况下收取2.41美金
     * 运费=重量kg*30
     * 体积重:长*宽*高/5000
     * 重量取实重和体积重的最大值
     *
     *
     * @return
     */
    @RequestMapping("compute")
    @ResponseBody
    public Map<String, String>  computeProfit() {
        double length = Double.valueOf(request.getParameter("length")); //长度
        double width =  Double.valueOf(request.getParameter("width")); //宽度
        double height =  Double.valueOf(request.getParameter("height")); //高度
        double cost = Double.valueOf(request.getParameter("cost")); //成本(RMB)
        double realWeight = Double.valueOf(request.getParameter("weight")); //实重
        double price = Double.valueOf(request.getParameter("price")); //定价
        //计算体积重:长*宽*高
        double volumeWeight = (length * width * height) /5000;
        //计算重量：取实重和体积重的最大值
        double weight = realWeight >= volumeWeight ? realWeight: volumeWeight;
        //计算亚马逊物流费用:根据高度来计算
        //大于等于1.7,则是3.19,小于则是2.41
        double amazonLogisticsCost = height >=1.7 ? 3.19 : 2.41;
        //计算利润
        double profit = price - price * 0.15 - amazonLogisticsCost - cost /6.6 - (weight * 30) / 6.6;
        DecimalFormat df = new DecimalFormat("#.00");
        String profitStr = df.format(profit);
        double perProfit = (profit / price) *100;
        String perProfitStr = df.format(perProfit);
        Map<String,String> map = new HashMap<>();
        map.put("profit",profitStr);
        map.put("perProfit",perProfitStr);
        return map;
    }
}
